!pip install yfinance
!pip install prophet
Requirement already satisfied: yfinance in /home/matthew/.local/lib/python3.8/site-packages (0.1.70) Requirement already satisfied: pandas>=0.24.0 in /home/matthew/.local/lib/python3.8/site-packages (from yfinance) (1.3.1) Requirement already satisfied: multitasking>=0.0.7 in /home/matthew/.local/lib/python3.8/site-packages (from yfinance) (0.0.10) Requirement already satisfied: lxml>=4.5.1 in /home/matthew/.local/lib/python3.8/site-packages (from yfinance) (4.6.4) Requirement already satisfied: requests>=2.26 in /home/matthew/.local/lib/python3.8/site-packages (from yfinance) (2.27.1) Requirement already satisfied: numpy>=1.15 in /home/matthew/.local/lib/python3.8/site-packages (from yfinance) (1.21.1) Requirement already satisfied: pytz>=2017.3 in /usr/lib/python3/dist-packages (from pandas>=0.24.0->yfinance) (2019.3) Requirement already satisfied: python-dateutil>=2.7.3 in /home/matthew/.local/lib/python3.8/site-packages (from pandas>=0.24.0->yfinance) (2.8.2) Requirement already satisfied: charset-normalizer~=2.0.0; python_version >= "3" in /home/matthew/.local/lib/python3.8/site-packages (from requests>=2.26->yfinance) (2.0.10) Requirement already satisfied: idna<4,>=2.5; python_version >= "3" in /usr/lib/python3/dist-packages (from requests>=2.26->yfinance) (2.8) Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3/dist-packages (from requests>=2.26->yfinance) (2019.11.28) Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/lib/python3/dist-packages (from requests>=2.26->yfinance) (1.25.8) Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2.7.3->pandas>=0.24.0->yfinance) (1.14.0) Requirement already satisfied: prophet in /home/matthew/.local/lib/python3.8/site-packages (1.0.1) Requirement already satisfied: Cython>=0.22 in /home/matthew/.local/lib/python3.8/site-packages (from prophet) (0.29.17) Requirement already satisfied: LunarCalendar>=0.0.9 in /home/matthew/.local/lib/python3.8/site-packages (from prophet) (0.0.9) Requirement already satisfied: cmdstanpy==0.9.68 in /home/matthew/.local/lib/python3.8/site-packages (from prophet) (0.9.68) Requirement already satisfied: convertdate>=2.1.2 in /home/matthew/.local/lib/python3.8/site-packages (from prophet) (2.3.2) Requirement already satisfied: holidays>=0.10.2 in /home/matthew/.local/lib/python3.8/site-packages (from prophet) (0.12) Requirement already satisfied: matplotlib>=2.0.0 in /home/matthew/.local/lib/python3.8/site-packages (from prophet) (3.5.0) Requirement already satisfied: numpy>=1.15.4 in /home/matthew/.local/lib/python3.8/site-packages (from prophet) (1.21.1) Requirement already satisfied: pandas>=1.0.4 in /home/matthew/.local/lib/python3.8/site-packages (from prophet) (1.3.1) Requirement already satisfied: pystan~=2.19.1.1 in /home/matthew/.local/lib/python3.8/site-packages (from prophet) (2.19.1.1) Requirement already satisfied: python-dateutil>=2.8.0 in /home/matthew/.local/lib/python3.8/site-packages (from prophet) (2.8.2) Requirement already satisfied: setuptools-git>=1.2 in /home/matthew/.local/lib/python3.8/site-packages (from prophet) (1.2) Requirement already satisfied: tqdm>=4.36.1 in /home/matthew/.local/lib/python3.8/site-packages (from prophet) (4.62.3) Requirement already satisfied: pytz in /usr/lib/python3/dist-packages (from LunarCalendar>=0.0.9->prophet) (2019.3) Requirement already satisfied: ephem>=3.7.5.3 in /home/matthew/.local/lib/python3.8/site-packages (from LunarCalendar>=0.0.9->prophet) (4.1.3) Requirement already satisfied: ujson in /home/matthew/.local/lib/python3.8/site-packages (from cmdstanpy==0.9.68->prophet) (5.1.0) Requirement already satisfied: pymeeus<=1,>=0.3.13 in /home/matthew/.local/lib/python3.8/site-packages (from convertdate>=2.1.2->prophet) (0.5.11) Requirement already satisfied: korean-lunar-calendar in /home/matthew/.local/lib/python3.8/site-packages (from holidays>=0.10.2->prophet) (0.2.1) Requirement already satisfied: hijri-converter in /home/matthew/.local/lib/python3.8/site-packages (from holidays>=0.10.2->prophet) (2.2.2) Requirement already satisfied: pillow>=6.2.0 in /home/matthew/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->prophet) (8.4.0) Requirement already satisfied: kiwisolver>=1.0.1 in /home/matthew/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->prophet) (1.3.2) Requirement already satisfied: packaging>=20.0 in /home/matthew/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->prophet) (21.3) Requirement already satisfied: setuptools-scm>=4 in /home/matthew/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->prophet) (6.3.2) Requirement already satisfied: cycler>=0.10 in /home/matthew/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->prophet) (0.11.0) Requirement already satisfied: pyparsing>=2.2.1 in /home/matthew/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->prophet) (3.0.6) Requirement already satisfied: fonttools>=4.22.0 in /home/matthew/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->prophet) (4.28.1) Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2.8.0->prophet) (1.14.0) Requirement already satisfied: tomli>=1.0.0 in /home/matthew/.local/lib/python3.8/site-packages (from setuptools-scm>=4->matplotlib>=2.0.0->prophet) (1.2.2) Requirement already satisfied: setuptools in /home/matthew/.local/lib/python3.8/site-packages (from setuptools-scm>=4->matplotlib>=2.0.0->prophet) (59.5.0)
import numpy as np
from sklearn.metrics import *
import pandas as pd
import yfinance as yf
import torch
import plotly.graph_objects as go
import plotly.figure_factory as ff
import pandas as pd
import pickle
from prophet import Prophet
This downloads OCHLV (Open Close High Low Volume) daily stock data from Facebook (META). This also, asigns the adjusted stock price to the target variable y
start ='2010-01-01'
end = '2022-02-01'
ticker = "FB"
target_col = "Adj Close"
x = yf.download(ticker, start=start, end=end)
y = x.loc[:,target_col]
[*********************100%***********************] 1 of 1 completed
fig = go.Figure()
fig.add_trace(go.Scatter(x=y.index, y=y.values,
mode='lines+markers',
name='Observed'))
title = "Facebooks Adjusted Closing Price"
fig.update_layout( title= title, paper_bgcolor="LightSteelBlue",
width=1000,height=600, autosize=False)
fig.show()
## Data Splitting
training_cutoff = "9-1-2021"
training = y[y.index < training_cutoff ]
testing = y[y.index >= training_cutoff ]
# creates a data frame for input to the Prophet Model
training_prophet = training.reset_index()
training_prophet.columns = ['ds', 'y']
change_points = ['2018-07-25', '2018-12-24', '2020-01-16', '2020-03-16', '2020-08-26', '2021-03-08']
## initialized the model
m = Prophet(daily_seasonality=True , changepoint_prior_scale=0.1 ,n_changepoints=10 )
## Adds Holidays
#m.add_country_holidays(country_name='US')
m.fit(training_prophet)
future = m.make_future_dataframe(periods=180)
forecast = m.predict(future)
forecast.loc[:, ['ds', 'yhat']].head()
Initial log joint probability = -51.0317
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
99 6625.46 0.017795 687.174 1 1 124
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
199 6782.59 0.00435874 1078.82 1 1 236
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
299 6834.93 0.00145874 107.685 1 1 342
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
399 6849.51 0.0174907 845.156 1 1 455
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
499 6855.33 0.000692265 33.7395 1 1 564
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
599 6856.2 0.00405243 189.42 1 1 681
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
699 6856.94 0.000250654 93.3902 0.2868 1 791
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
799 6857.13 0.00200194 57.4016 1 1 899
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
899 6857.35 6.49104e-05 8.34185 1 1 1013
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
917 6857.35 9.11022e-07 17.0082 0.03777 1 1037
Optimization terminated normally:
Convergence detected: relative gradient magnitude is below tolerance
| ds | yhat | |
|---|---|---|
| 0 | 2012-05-18 | 20.705632 |
| 1 | 2012-05-21 | 20.661848 |
| 2 | 2012-05-22 | 21.002973 |
| 3 | 2012-05-23 | 21.021684 |
| 4 | 2012-05-24 | 21.117970 |
preds = forecast.set_index('ds').loc[:, 'yhat'].loc[testing.index]
preds
corr = np.corrcoef(testing, preds)[0,1]
mape = mean_absolute_percentage_error(testing, preds)
corr, mape
(-0.6561231097033039, 0.0880160041844541)
Results of the forecasting are evaluated on the test data
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(x=y.index, y=y.values,
mode='lines+markers',
name='Observed'))
fig.add_trace(go.Scatter(x=forecast.loc[:, 'ds'],
y=forecast.loc[:, 'yhat'],
mode='lines',
opacity =.5,
name='Prophet Forecasted Values'))
title = F"Forecasting results Facebook "
fig.add_annotation(x=pd.to_datetime('9-24-2021'), y=380,
text="BBC Publishes 5 Things Leaked Documents Reveal <br>'The Facebook Files'",
showarrow=True,
arrowhead=1)
fig.update_layout( title= title, paper_bgcolor="LightSteelBlue",
width=1000,height=600, autosize=False)
fig.show()
# Python
from prophet.plot import plot_plotly, plot_components_plotly
fig = plot_plotly(m, forecast)
fig.add_trace(go.Scatter(x=y.index, y=y.values,
mode='lines',
name='Observed'))
title = F"Forecasting results Facebook "
fig.update_layout( title= title, paper_bgcolor="LightSteelBlue",
width=1000,height=600, autosize=False)
fig.show()
fig2 = m.plot_components(forecast)